package com.ff.neuropp.controller;


import com.ff.neuropp.domain.Career;
import com.ff.neuropp.service.CareerService;
import com.ff.neuropp.util.ResultTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/career")
@Api(value = "职业生涯管理controller", tags = {"职业生涯管理接口"})
public class PcCareerController {

    @Autowired
    private CareerService careerService;



    @PostMapping("/create")
    @ApiOperation(value = "添加职业生涯信息",notes = "添加职业生涯信息")
    public Map<String,Object> add(@RequestBody Career career){
        Career type = careerService.save(career);
        return ResultTool.genSuccessResult(type);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改职业生涯信息", notes = "修改职业生涯信息")
    public Map<String,Object> updateCourseCategory(@RequestBody Career career){
        Career cc = careerService.findById(career.getId());
        if(null == cc){ // 没查到该条数据
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,NO Data！");
        }
        Career updateCc = careerService.save(career);
        return ResultTool.genSuccessResult("Success",updateCc);
    }

    @GetMapping("/findbyid")
    @ApiOperation(value = "根据id查询信息(PC)", notes = "根据id查询信息")
    public Map<String, Object> findById(@ApiParam(required = true, value = "序号") @RequestParam Integer id,
                                        HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Career career = careerService.findById(id);
        if (null == career) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }
        return ResultTool.genSuccessResult("success",career);
    }

    @GetMapping("/listpage")
    @ApiOperation(value = "职业生涯分页", notes = "职业生涯分页")
    public Map<String, Object> findListPage(@Valid Career career,HttpServletRequest request) {
        Page<Career> list = careerService.findPage(career);
        Map<String, Object> result4Page = new HashMap<>();
        result4Page.put("total",list.getTotalPages());
        result4Page.put("pageNo",list.getNumber()+1);
        result4Page.put("list",list.getContent());
        if(null !=list && list.getTotalElements()>0){
            return ResultTool.genSuccessResult("Success",result4Page);
        }else{
            return ResultTool.genSuccessResult("Error,NO Data！",result4Page);
        }

    }

    @GetMapping("/list")
    @ApiOperation(value = "获取所有信息", notes = "获取所有信息")
    public Map<String, Object> findList(@ApiParam(required = false, value = "关键词") @RequestParam(required = false) String name,
                                        HttpServletRequest request) {
        Map<String, Object> param = new HashMap<>();
        param.put("GT_id", 0);
        if (null != name && name.length() > 0) {
            param.put("LIKE_careerName", name);
        }
        List<Career> list = careerService.findList(param);
        if (null != list && list.size() > 0) {
            Map<String, Object> result = new HashMap<>();
            result.put("total",list.size());
            result.put("list",list);
            return ResultTool.genSuccessResult("success",result);
        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"Error,no data！");
        }
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除职业生涯信息", notes = "删除职业生涯信息")
    public  Map<String,Object> deleteCourseCategory(@ApiParam(required = true,value = "职业生涯类型id") @RequestParam Integer id){
        Career cc = careerService.findById(id);
        if (null == cc) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "Error,NO Data！");
        }
        careerService.deleteById(id);
        return ResultTool.genSuccessResult("Success");

    }




}
